package vn.com.vtv.weather;

import vn.com.vtv.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.Spinner;

public class SettingActivity extends Activity{
	private Spinner spinner1;
	private ImageView imvBg;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		imvBg = (ImageView) findViewById(R.id.imv_bg);
		spinner1 = (Spinner) findViewById(R.id.spinner1);
		spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener(
				spinner1));
		spinner1.setSelection(VTVApplication.prefs.getInt(
				VTVApplication.KEY_BG_INDEX, 0));
		imvBg.setImageResource(VTVApplication.prefs.getInt(
				VTVApplication.KEY_BG_INDEX, 0) == 0 ? R.drawable.bg_00
				: R.drawable.bg_01);
	}
	
	public class CustomOnItemSelectedListener implements OnItemSelectedListener {
		Spinner mSpinner;

		public CustomOnItemSelectedListener(Spinner mSpinner) {
			this.mSpinner = mSpinner;
		}

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			if (mSpinner.getId() == spinner1.getId()) {
				switch (pos) {
				case 0:
					VTVApplication.prefs.edit()
							.putInt(VTVApplication.KEY_BGID, R.drawable.bg_00)
							.commit();
					break;
				case 1:
					VTVApplication.prefs.edit()
							.putInt(VTVApplication.KEY_BGID, R.drawable.bg_01)
							.commit();
					break;
				default:
					break;
				}
				VTVApplication.prefs.edit()
						.putInt(VTVApplication.KEY_BG_INDEX, pos).commit();
				imvBg.setImageResource(pos == 0 ? R.drawable.bg_00
						: R.drawable.bg_01);

			} 
			if (WidgetProvider._provider != null)
				WidgetProvider._provider.onUpdate(null, null, null);
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
		}

	}
}
